package com.cyt.web.controller.wx;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.wx.dao.WxMemberDao;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.model.vo.WxMemberVo;
import com.cyt.wx.service.WxMemberService;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信端-会员管理控制层
 *
 * @author caideyong
 * @date 2019-7-9 8:37:55
 */
@RestController
public class WxMemberController extends BaseController {

    @Resource
    private WxMemberService wxMemberService;
    @Autowired
    private WxMemberDao wxMemberDao;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @RequestMapping("/wx/wxmember/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页查询(管理员)
     *
     * @param wxMemberVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/page", method = RequestMethod.POST)
    public ReturnResult page(WxMemberVo wxMemberVo, int page, int limit) {
        return wxMemberService.page(wxMemberVo, page, limit);
    }

    /**
     * 分页查询(用户)
     *
     * @param wxMemberVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/pageUser", method = RequestMethod.POST)
    public ReturnResult pageUser(HttpServletRequest request,WxMemberVo wxMemberVo, int page, int limit) {
        SysUser sysUser=acceptCurrentUser(request);
        wxMemberVo.setUserId(sysUser.getId());
        return wxMemberService.pageUser(wxMemberVo, page, limit,sysUser);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/wx/wxmember/wxMemberEdit");
        if (StrUtil.isNotEmpty(id)) {
            WxMember wxMember=wxMemberService.selectByPrimaryKey(id);
            if(wxMember!=null&& StringUtils.isNotEmpty(wxMember.getNickName())){
                wxMember.setNickName(BaseUtil.decodeBase64(wxMember.getNickName()));
            }
            view.addObject("wxMember",wxMember);
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxMemberVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxMemberVo wxMemberVo) {
        return wxMemberService.addOrUpdate(wxMemberVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wxMemberVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxMemberVo wxMemberVo) {
        return wxMemberService.remove(wxMemberVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wxMemberVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @BaseLog(module = "微信端", classz = "会员管理", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxmember/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxMemberVo wxMemberVo) {
        return wxMemberService.delete(wxMemberVo, acceptCurrentUser(request));
    }
}